import { NextResponse } from 'next/server';
import { DeepSeekUserPhone } from '@/app/lib/model'
import { connectToDatabase } from '@/app/lib/mongodb'

export async function POST(req: Request) {
    const { phone } = await req.json()
    const formattedPhone = Number(phone); 
    if (Number.isNaN(formattedPhone)) {
        return NextResponse.json({ code: 401, msg: '手机号格式不正确' });
    }

    try {
        await connectToDatabase()

        const user = await DeepSeekUserPhone.findOne({ phone: formattedPhone });
        if (!user) {
            return NextResponse.json({ code: 401, msg: '手机号不存在' });
        }
        return NextResponse.json({ code: 200, msg: '手机号存在', data: user });
    } catch (err) { 
        return NextResponse.json(
            { code: 500, msg: '服务器错误' },
            { status: 500 }
        );
    }
}